package com.s2paganini.util
{
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;	

	/**
	 * @author   siLvio paganini | s2paganini.com
	 * @category utils
	 */
	public class Utils 
	{
		/**
		 * @param  wichEmail Which email to validate
		 * @return Result of validation <code>Boolean</code>
		 */
		public static function checkEmail(wichEmail : String) : Boolean 
		{
			var regExpLine : RegExp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]{2,64}(\.[a-z0-9-]{2,64}){0,1}\.[a-z]{2,4}$/;
			
			var result : Object = regExpLine.exec(wichEmail);
			return (!(result == null));
		}

		/**
		 * @param  number Number format
		 * @param  n Zero Format
		 * @return Number Formated
		 */
		public static function zeroFormat(number : Number, n : Number = 2) : String
		{
			if (number.toString().length < n) {
				
				var numberConverted : String = "";
				
				for (var i : int = 1;i < n; i++) {
					numberConverted += "0";
				}
				
				numberConverted = numberConverted + number;

				return numberConverted;
			} else {
				return n.toString();
			}
		}

		/**
		 * @param  web URL to open       
		 * @param  winName Window Name
		 * @param  wid Window Width
		 * @param  hei Window Height
		 * @param  scrollbars Window scrollbars
		 * @return void
		 */
		public static function newPopUp(web : String, winName : String, wid : Number, hei : Number, scrollbars : Number) : void
		{
			var urlToCall : URLRequest = new URLRequest("javascript:window.open('" + web + "','" + winName + "','width=" + wid + ",height=" + hei + ",scrollbars=" + scrollbars + "');void(0);");
			navigateToURL(urlToCall);
		}

		/**
		 * @param  arrayToMix Array to randomize
		 * @return Array randomized
		 */
		public static function ArrayMix(arrayToMix : Array) : Array 
		{
			var randomizedArr : Array = new Array();
			var numElementsLeft : int = arrayToMix.length;
			
			while (numElementsLeft) {
				
				var index : int = Math.floor(Math.random() * numElementsLeft);
				
				randomizedArr.push(arrayToMix[index]);
				
				arrayToMix.splice(index, 1);
				numElementsLeft--;
			}
			
			for (var i : int = 0;i < randomizedArr.length; i++) {
				arrayToMix[i] = randomizedArr[i];
			}
			
			return arrayToMix;
		}

		/**
		 * @param target Graphics of DisplayObject to draw
		 * @param w width
		 * @param h height
		 * @param col color
		 * @param a alpha
		 * @param startX
		 * @param startY
		 * @param bitmapFill name of the BitmapData to be a pattern
		 */
		static public function drawSquareGraphic(graphics : Graphics, w : Number, h : Number, col : Number = 0x000000, a : Number = 1, startX : Number = 0, startY : Number = 0, bitmapFill : BitmapData = null) : void 
		{
			if(col == -1) {
				graphics.beginBitmapFill(bitmapFill, null, true, true);	
			} else {
				graphics.beginFill(col, a);
			}
			graphics.drawRect(startX, startY, w, h);
			graphics.endFill();
		}
		
		/**
		 * @param target Graphics of DisplayObject to draw
		 * @param w width
		 * @param h height
		 * @param col color
		 * @param a alpha
		 */
		static public function drawTriangle(graphics : Graphics, w: Number, h : Number, col : Number = 0, a : Number = 1) : void
		{
			graphics.beginFill(col, a);
			graphics.lineTo(0, w);
			graphics.lineTo(h, w/2);
			graphics.lineTo(h, w/2);
		}

		/**
		 * @param url URL to open
		 * @param target HTML target
		 */
		static public function getURL(url : String, target : String = "_self") : void
		{
			navigateToURL(new URLRequest(url), target);
		}

		/**
		 * @param arg XML Param to verify whether it's true or false
		 */
		static public function getXMLBoolean(arg : *) : Boolean
		{
			return (arg == 1 || arg == "1" || arg == "true" || arg == true);
		}

		/**
		 * @param whichArray Target Array to delete the item
		 * @param item which Item to delete
		 * @param paramItemArray item's param (default null)  
		 */
		static public function deleteArrayItem(whichArray : Array, item : *, paramItemArray : String = null, paramItemArrayToCompare : * = null) : Array
		{
			var resultArray : Array = new Array();
			
			for (var i : int = 0;i < whichArray.length; i++) {
				if(paramItemArrayToCompare) {
					if(whichArray[i][paramItemArray] != paramItemArrayToCompare) {
						resultArray.push(whichArray[i]);
					}	
				} else if(paramItemArray) {
					if(whichArray[i][paramItemArray] != item[paramItemArray]) {
						resultArray.push(whichArray[i]);
					}
				} else {
					if(whichArray[i] != item) {
						resultArray.push(whichArray[i]);
					}	
				}
			}
			
			return resultArray;
		} 

		/**
		 * Push unique values to the selected array
		 * @param whichArray reference Array
		 * @param item Item to push 
		 */
		static public function pushUnique(whichArray : Array, item : *) : Array
		{
			var tempFound : int = 0;
			
			for each(var i : int in whichArray) {
				if(whichArray[i] == item) {
					tempFound++;	
				}
			}
			
			if(tempFound < 1) whichArray.push(item);
		
			return whichArray;
		}
	}
}
